package com.dg11185.nearshop.activity.shop;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;

import com.android.nearshop.R;
import com.dg11185.libs.network.NetClient;
import com.dg11185.nearshop.activity.SubPageActivity;
import com.dg11185.nearshop.app.ConvertUnit;
import com.dg11185.nearshop.app.MyApplication;
import com.dg11185.nearshop.app.ViewUtils;
import com.dg11185.nearshop.net.request.GeneralRequest;
import com.dg11185.nearshop.net.response.NetStatus;

public class AddCommentsActivity extends SubPageActivity {

    private RatingBar mainBar, tasteBar, envirBar, serviceBar;

    private EditText content;

    private String merchantId;

    private Dialog submitDialog ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shop_comment_add);
         
        setSubPageTitle("添加评论");

        mainBar = (RatingBar) this.findViewById(R.id.comment_main_rating_bar);
        tasteBar = (RatingBar) this.findViewById(R.id.comment_taste_rating_bar);
        envirBar = (RatingBar) this.findViewById(R.id.comment_envir_rating_bar);
        serviceBar = (RatingBar) this.findViewById(R.id.comment_service_rating_bar);

        mainBar.setRating(5);
        tasteBar.setRating(5);
        envirBar.setRating(5);
        serviceBar.setRating(5);
        content = (EditText) this.findViewById(R.id.comment_content);
        merchantId = getIntent().getStringExtra("merchantId");

        submitDialog = ViewUtils.createSubmittingDialog(this);
        if (getIntent().getBooleanExtra("isDietShop", false)) {
            TextView txtView = (TextView) this.findViewById(R.id.comment_title);
            findViewById(R.id.comment_taste_layout).setVisibility(View.VISIBLE);
            txtView.setText("请你分别对口味、环境和服务评分吧");
        }
    }

    public void onSubmitCommentClicked(View v) {
        float main = mainBar.getRating();
        float taste = tasteBar.getRating();
        float envir = envirBar.getRating();
        float service = serviceBar.getRating();

        final String txt = content.getEditableText().toString().trim();
        if (main == 0) {
            ViewUtils.showToast(this, "请给商家一个总体评分吧...");
            return;
        }
        if ("".equals(txt)) {
            ViewUtils.showToast(this, "评论内容不能为空哦~~");
            return;
        }
        if (txt.length() > 140) {
            ViewUtils.showToast(this, "评论内容不要太长哦~~");
            return;
        }
        GeneralRequest request = new GeneralRequest(GeneralRequest.ADD_COMMENT)
                .add("userId", MyApplication.getUser().userId).add("userName", ConvertUnit.getShortUserName()).add("content", txt)
                .add("merchantId", merchantId);

        if (main > 0) {
            request.add("starRating", taste + "");
        }
        if (taste > 0) {
            request.add("tasteEvaluation", taste + "");
        }
        if (envir > 0) {
            request.add("environmentalAssessment", envir + "");
        }
        if (service > 0) {
            request.add("serviceEvaluation", service + "");
        }

        submitDialog.show();
        request.encryption();
        NetClient.httpPost(request);
        request.setActionListener(new GeneralRequest.ActionListener<NetStatus>() {

            @Override
            public void onSuccess(NetStatus result) {
                submitDialog.dismiss();
                if (result == NetStatus.SUCCESS) {
                    ViewUtils.showToast(AddCommentsActivity.this, "评论成功...");
                    Intent intent = new Intent();
                    AddCommentsActivity.this.setResult(RESULT_OK, intent);
                    AddCommentsActivity.this.finish();
                } else {
                    onFailure(-1);
                }
            }

            @Override
            public void onFailure(int resultCode) {
                submitDialog.dismiss();
                if (resultCode == NetClient.NET_DISCONNECT_CODE) {
                    ViewUtils.showToast(AddCommentsActivity.this, R.string.cant_connect_network);
                } else {
                    ViewUtils.showToast(AddCommentsActivity.this, "发布失败...");
                }

            }
        });
    }

}
